﻿using EasyCaching.Core;

namespace LJ.Cache.EasyCaching.Internal;

public class EasyCachingCache(IEasyCachingProviderFactory easyCachingProviderFactory) : ICache
{
    private readonly IEasyCachingProviderFactory _easyCachingProviderFactory = easyCachingProviderFactory;


    public async Task SetAsync<T>(string key, T value, TimeSpan expiration, CancellationToken cancellationToken = default)
    {
        var easyCachingProvider = _easyCachingProviderFactory.GetCachingProvider(EasyCachingConst.Key);
        await easyCachingProvider.SetAsync(key, value, expiration, cancellationToken);
    }

    public async Task<CacheResult<T>> GetAsync<T>(string key, CancellationToken cancellationToken = default)
    {
        var easyCachingProvider = _easyCachingProviderFactory.GetCachingProvider(EasyCachingConst.Key);
        var cache = await easyCachingProvider.GetAsync<T>(key, cancellationToken);
        return new CacheResult<T>(cache.HasValue, cache.Value);
    }

}